package com.biostime.qdingding.modules.cms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.cms.entity.Category;
import com.biostime.qdingding.modules.cms.service.CategoryService;
import com.biostime.qdingding.modules.cms.service.ChannelService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 栏目表Controller
 * @author king
 * @version 2015-12-11
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/category")
public class CategoryController extends BaseController {

	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ChannelService channelService;
	
	@ModelAttribute
	public Category get(@RequestParam(required=false) String id) {
		Category entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = categoryService.get(id);
		}
		if (entity == null){
			entity = new Category();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:category:view")
	@RequestMapping(value = {"list", ""})
	public String list(Category category, HttpServletRequest request, HttpServletResponse response, Model model) {
//		Integer channelId = null ;
//		channelId = isAdmission(request, channelId, model);
//		if(channelId ==null){
//			addMessage(model, "频道参数错误,未从正常的入口进入");
//			return  "modules/cms/categoryList";
//		}
		category.setChannelId(0);
		Page<Category> page = categoryService.findPage(new Page<Category>(request, response), category); 
		model.addAttribute("page", page);
		return "modules/cms/categoryList";
	}

	@RequiresPermissions("cms:category:view")
	@RequestMapping(value = "form")
	public String form(Category category,HttpServletRequest request, Model model) {
//		Integer channelId = null ;
//		channelId = isAdmission(request, channelId, model);
//		if(channelId ==null){
//			model.addAttribute("message", "频道参数错误,未从正常的入口进入");
//			return  "modules/cms/categoryForm";
//		}
		
		List<Category> parentList = categoryService.getParentsTree();
		
		model.addAttribute("parentList", parentList);
		model.addAttribute("category", category);
		return "modules/cms/categoryForm";
	}

	@RequiresPermissions(value={"cms:category:edit","cms:category:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Category category, Model model, HttpServletRequest request,  RedirectAttributes redirectAttributes) {
		//数据检测未通过 保存通过...
		if (!beanValidator(model ,category)){
			return form(category, request, model);
		}
//		Integer channelId = null ;
//		channelId = isAdmission(request, channelId, model);
//		if(channelId ==null){
//			model.addAttribute("message", "频道参数错误,未从正常的入口进入");
//			return "modules/cms/categoryForm";
//		}
		
		User curUser = UserUtils.getUser();
		
		category.setCenter(curUser.getCenter());
		category.setChannelId(0);
		categoryService.save(category);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/cms/category/?repage";
	}
	
	@RequiresPermissions("cms:category:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Category category, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				categoryService.delete(new Category(id));
			}
		}else{
			categoryService.delete(category);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/cms/category/?repage";
	}

	//检测channelId有没有错误
//		public Integer isAdmission(HttpServletRequest request, Integer channelId, Model model){
//			try {
//				HttpSession session = request.getSession();
//				if(request.getParameter("channel_id") != null && !request.getParameter("channel_id").equalsIgnoreCase(""))	{
//					channelId = Integer.valueOf(request.getParameter("channel_id"));
//					Channel channel = new Channel(String.valueOf(channelId));
//					if(channelService.findList(channel).size() <= 0){
//						channelId = null;
//						return channelId;
//					}
//					session.setAttribute("channelId", channelId);
//				}
//			            
//				channelId = (Integer) session.getAttribute("channelId");
//			} catch (Exception e) {
//			}
//			return channelId;
//		}
}